package com.example.oldBookTest.service;

import cn.hutool.core.lang.Dict;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.example.oldBookTest.utils.BookTraceUtils;
import com.example.oldBookTest.utils.WeBASEUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class BookTraceService {
    @Autowired
    WeBASEUtils weBASEUtils;

    public Dict getAllBooksForUserId(String addr, int userId) {
        List paramList = new ArrayList();
        paramList.add(userId);

        Dict reqResult = weBASEUtils.commonReq(addr
                , "getAllBooksForUserId"
                , paramList
                , BookTraceUtils.ABI
                , BookTraceUtils.contractName
                , BookTraceUtils.contractAdress);
        JSONArray resArr = JSONUtil.parseArray(reqResult.get("result"));

        JSONArray ids = (JSONArray) resArr.get(0);
        JSONArray bookNames = (JSONArray) resArr.get(1);
        JSONArray usages = (JSONArray) resArr.get(2);
        JSONArray pageCounts = (JSONArray) resArr.get(3);
        JSONArray photoUrls = (JSONArray) resArr.get(4);
        JSONArray bookCodes = (JSONArray) resArr.get(5);
        JSONArray traceCode = (JSONArray) resArr.get(6);
        JSONArray categories = (JSONArray) resArr.get(7);
        JSONArray bookCarbonPoints = (JSONArray) resArr.get(8);
        JSONArray donorIds = (JSONArray) resArr.get(9);
        JSONArray pubAddresses = (JSONArray) resArr.get(10);

        Dict res = Dict.create();
        List bookList = new ArrayList();

        for (int i = 0; i < ids.size(); i++) {

            Dict item = new Dict()
                    .set("id", ids.getInt(i))
                    .set("bookName", bookNames.get(i))
                    .set("usage", usages.get(i))
                    .set("pageCount", pageCounts.get(i))
                    .set("photoUrl", photoUrls.get(i))
                    .set("bookCode", bookCodes.get(i))
                    .set("traceCode", traceCode.get(i))
                    .set("category", categories.get(i))
                    .set("bookCarbonPoint", bookCarbonPoints.get(i))
                    .set("donorId", donorIds.get(i))
                    .set("pubAddress", pubAddresses.get(i));
            bookList.add(item);
        }

        res.set("data", bookList);
        res.set("status", 200);

        return res;

    }

    public Dict registerBook(String addr
            , String book_name
            , String usages
            , int page_count
            , String photo_url
            , String books_code
            , String trace_code
            , String category
            , int book_carbon_points
            , int donorId
            , String pub_address) {
        List paramList = new ArrayList();

        paramList.add(book_name);
        paramList.add(usages);
        paramList.add(page_count);
        paramList.add(photo_url);
        paramList.add(books_code);
        paramList.add(trace_code);
        paramList.add(category);
        paramList.add(book_carbon_points);
        paramList.add(donorId);
        paramList.add(pub_address);

        String response = weBASEUtils.commonReqSimple(addr
                , "registerBook"
                , paramList
                , BookTraceUtils.ABI
                , BookTraceUtils.contractName
                , BookTraceUtils.contractAdress);

        JSONObject resObj = JSONUtil.parseObj(response);
        Dict res = Dict.create();

        res.set("data", resObj.getInt("output")); // 非view函数，返回结果在output里
        res.set("status", 200);

        return res;
    }

    public Dict registerDisinfection(String addr
            , int book_id
            , boolean is_disinfection
            , int disinfection_time
            , int count) {
        List paramList = new ArrayList();

        paramList.add(book_id);
        paramList.add(is_disinfection);
        paramList.add(disinfection_time);
        paramList.add(count);

        String response = weBASEUtils.commonReqSimple(addr
                , "registerDisinfection"
                , paramList, BookTraceUtils.ABI
                , BookTraceUtils.contractName
                , BookTraceUtils.contractAdress);

        JSONObject resObj = JSONUtil.parseObj(response);
        Dict res = Dict.create();

        res.set("data", resObj.getInt("output")); // 非view函数，返回结果在output里
        res.set("status", 200);

        return res;
    }


    //历届用户对未来受捐者的留言
    public Dict createMsg(String addr
            , int book_id
            , String message
            , int user_id) {
        List paramList = new ArrayList();

        paramList.add(book_id);
        paramList.add(message);
        paramList.add(user_id);

        String response = weBASEUtils.commonReqSimple(addr
                , "createMsg"
                , paramList
                , BookTraceUtils.ABI
                , BookTraceUtils.contractName
                , BookTraceUtils.contractAdress);

        JSONObject resObj = JSONUtil.parseObj(response);
        Dict res = Dict.create();

        res.set("data", resObj.getInt("output")); // 非view函数，返回结果在output里
        res.set("status", 200);

        return res;
    }

    //用户捐赠书籍后增加用户的碳积分总数，以及增加用户的捐赠数量
    public Dict updateDonationAndPoints(String addr, int donorId, int book_carbon_points) {
        List paramList = new ArrayList();

        paramList.add(donorId);
        paramList.add(book_carbon_points);

        String response = weBASEUtils.commonReqSimple(addr
                , "updateDonationAndPoints"
                , paramList
                , BookTraceUtils.ABI
                , BookTraceUtils.contractName
                , BookTraceUtils.contractAdress);

        JSONObject resObj = JSONUtil.parseObj(response);
        Dict res = Dict.create();

        res.set("data", resObj.getInt("output")); // 非view函数，返回结果在output里
        res.set("status", 200);

        return res;
    }

    //注册物流信息
    public Dict registerTransit(String addr, String code, boolean isReceived) {
        List paramList = new ArrayList();

        paramList.add(code);
        paramList.add(isReceived);

        String response = weBASEUtils.commonReqSimple(addr
                , "registerTransit"
                , paramList
                , BookTraceUtils.ABI
                , BookTraceUtils.contractName
                , BookTraceUtils.contractAdress);

        JSONObject resObj = JSONUtil.parseObj(response);
        Dict res = Dict.create();

        res.set("data", resObj.getInt("output")); // 非view函数，返回结果在output里
        res.set("status", 200);

        return res;
    }

    //注册回收信息
    public Dict registerRecycle(String addr, int transitIds, String description) {
        List paramList = new ArrayList();

        paramList.add(transitIds);
        paramList.add(description);

        String response = weBASEUtils.commonReqSimple(addr
                , "registerRecycle"
                , paramList, BookTraceUtils.ABI
                , BookTraceUtils.contractName
                , BookTraceUtils.contractAdress);

        JSONObject resObj = JSONUtil.parseObj(response);
        Dict res = Dict.create();

        res.set("data", resObj.getInt("output")); // 非view函数，返回结果在output里
        res.set("status", 200);

        return res;
    }

    //注册需求信息
    public Dict registerDemand(String addr, String content, boolean state, int userId) {
        List paramList = new ArrayList();

        paramList.add(content);
        paramList.add(state);
        paramList.add(userId);

        String response = weBASEUtils.commonReqSimple(addr
                , "registerDemand"
                , paramList
                , BookTraceUtils.ABI
                , BookTraceUtils.contractName
                , BookTraceUtils.contractAdress);

        JSONObject resObj = JSONUtil.parseObj(response);
        Dict res = Dict.create();

        res.set("data", resObj.getInt("output")); // 非view函数，返回结果在output里
        res.set("status", 200);

        return res;
    }

    public Dict registerOrder(String addr, int book_id, String remark) {
        List paramList = new ArrayList();

        paramList.add(book_id);
        paramList.add(remark);

        String response = weBASEUtils.commonReqSimple(addr
                , "registerOrder"
                , paramList
                , BookTraceUtils.ABI
                , BookTraceUtils.contractName
                , BookTraceUtils.contractAdress);

        JSONObject resObj = JSONUtil.parseObj(response);
        Dict res = Dict.create();

        res.set("data", resObj.getInt("output")); // 非view函数，返回结果在output里
        res.set("status", 200);

        return res;
    }

    public Dict confirmTransit(String addr, int transitId, boolean isReceived) {
        List paramList = new ArrayList();

        paramList.add(transitId);
        paramList.add(isReceived);

        String response = weBASEUtils.commonReqSimple(addr
                , "confirmTransit"
                , paramList
                , BookTraceUtils.ABI
                , BookTraceUtils.contractName
                , BookTraceUtils.contractAdress);

        JSONObject resObj = JSONUtil.parseObj(response);
        Dict res = Dict.create();

        res.set("data", resObj.getInt("output")); // 非view函数，返回结果在output里
        res.set("status", 200);

        return res;
    }

    public Dict carbonPointsConversion(String addr, int bookId, int userId) {
        List paramList = new ArrayList();

        paramList.add(bookId);
        paramList.add(userId);

        String response = weBASEUtils.commonReqSimple(addr
                , "carbonPointsConversion"
                , paramList
                , BookTraceUtils.ABI
                , BookTraceUtils.contractName
                , BookTraceUtils.contractAdress);

        JSONObject resObj = JSONUtil.parseObj(response);
        Dict res = Dict.create();

        res.set("data", resObj.getInt("output")); // 非view函数，返回结果在output里
        res.set("status", 200);

        return res;
    }

    public Dict BookTransfer(String addr, int donorId, int bookId) {
        List paramList = new ArrayList();

        paramList.add(donorId);
        paramList.add(bookId);

        String response = weBASEUtils.commonReqSimple(addr
                , "BookTransfer"
                , paramList
                , BookTraceUtils.ABI
                , BookTraceUtils.contractName
                , BookTraceUtils.contractAdress);

        JSONObject resObj = JSONUtil.parseObj(response);
        Dict res = Dict.create();

        res.set("data", resObj.getInt("output")); // 非view函数，返回结果在output里
        res.set("status", 200);

        return res;
    }

    public Dict getBookforId(String addr, int bookId) {
        List paramList = new ArrayList();
        paramList.add(bookId);

        Dict reqResult = weBASEUtils.commonReq(addr
                , "getBookforId"
                , paramList
                , BookTraceUtils.ABI
                , BookTraceUtils.contractName
                , BookTraceUtils.contractAdress);
        JSONArray resArr = JSONUtil.parseArray(reqResult.get("result"));

        Dict res = Dict.create();
        res.set("id", resArr.getInt(0));
        res.set("book_name", resArr.getStr(1));
        res.set("usages", resArr.getStr(2));
        res.set("page_count", resArr.getInt(3));
        res.set("photo_url", resArr.getStr(4));
        res.set("books_code", resArr.getStr(5));
        res.set("trace_code", resArr.getStr(6));
        res.set("category", resArr.getStr(7));
        res.set("book_carbon_points", resArr.getInt(8));
        res.set("donorId", resArr.getInt(9));
        res.set("pub_address", resArr.getStr(10));
        res.set("status", 200);

        return res;
    }

    public Dict getTransitforId(String addr, int transitId) {
        List paramList = new ArrayList();
        paramList.add(transitId);

        Dict reqResult = weBASEUtils.commonReq(addr
                , "getTransitforId"
                , paramList
                , BookTraceUtils.ABI
                , BookTraceUtils.contractName
                , BookTraceUtils.contractAdress);
        JSONArray resArr = JSONUtil.parseArray(reqResult.get("result"));

        Dict res = Dict.create();
        res.set("id", resArr.getInt(0));
        res.set("code", resArr.getStr(1));
        res.set("isReceived", resArr.getBool(2));
        res.set("status", resArr.getBool(3));

        return res;
    }

    public Dict getRecycleforId(String addr, int recycleId) {
        List paramList = new ArrayList();
        paramList.add(recycleId);

        Dict reqResult = weBASEUtils.commonReq(addr
                , "getRecycleforId"
                , paramList
                , BookTraceUtils.ABI
                , BookTraceUtils.contractName
                , BookTraceUtils.contractAdress);
        JSONArray resArr = JSONUtil.parseArray(reqResult.get("result"));

        Dict res = Dict.create();
        res.set("id", resArr.getInt(0));
        res.set("transitIds", resArr.getInt(1));
        res.set("description", resArr.getStr(2));
        res.set("created", resArr.getInt(3));
        res.set("status", 200);

        return res;
    }

    public Dict getDemandforId(String addr, int demandId) {
        List paramList = new ArrayList();
        paramList.add(demandId);

        Dict reqResult = weBASEUtils.commonReq(addr
                , "getDemandforId"
                , paramList, BookTraceUtils.ABI
                , BookTraceUtils.contractName
                , BookTraceUtils.contractAdress);
        JSONArray resArr = JSONUtil.parseArray(reqResult.get("result"));

        Dict res = Dict.create();
        res.set("id", resArr.getInt(0));
        res.set("content", resArr.getStr(1));
        res.set("state", resArr.getBool(2));
        res.set("userId", resArr.getInt(3));
        res.set("created", resArr.getInt(4));
        res.set("status", 200);

        return res;
    }

    public Dict getOrderforId(String addr, int orderId) {
        List paramList = new ArrayList();
        paramList.add(orderId);

        Dict reqResult = weBASEUtils.commonReq(addr
                , "getOrderforId"
                , paramList, BookTraceUtils.ABI
                , BookTraceUtils.contractName
                , BookTraceUtils.contractAdress);
        JSONArray resArr = JSONUtil.parseArray(reqResult.get("result"));

        Dict res = Dict.create();
        res.set("id", resArr.getInt(0));
        res.set("book_id", resArr.getInt(1));
        res.set("transitIds", resArr.getInt(2));
        res.set("remark", resArr.getStr(3));
        res.set("create_time", resArr.getInt(4));
        res.set("finish_time", resArr.getInt(5));
        res.set("status", 200);

        return res;
    }

    public Dict getBookMsgforId(String addr, int msgId) {
        List paramList = new ArrayList();
        paramList.add(msgId);

        Dict reqResult = weBASEUtils.commonReq(addr
                , "getBookMsgforId"
                , paramList
                , BookTraceUtils.ABI
                , BookTraceUtils.contractName
                , BookTraceUtils.contractAdress);
        JSONArray resArr = JSONUtil.parseArray(reqResult.get("result"));

        Dict res = Dict.create();
        res.set("id", resArr.getInt(0));
        res.set("book_id", resArr.getInt(1));
        res.set("message", resArr.getStr(2));
        res.set("create_time", resArr.getInt(3));
        res.set("user_id", resArr.getInt(4));
        res.set("status", 200);

        return res;
    }

    public Dict getBookMsgforBookId(String addr, int bookId) {
        List paramList = new ArrayList();
        paramList.add(bookId);

        Dict reqResult = weBASEUtils.commonReq(addr
                , "getBookMsgforBookId"
                , paramList
                , BookTraceUtils.ABI
                , BookTraceUtils.contractName
                , BookTraceUtils.contractAdress);
        JSONArray resArr = JSONUtil.parseArray(reqResult.get("result"));

        Dict res = Dict.create();
        res.set("id", resArr.getInt(0));
        res.set("book_id", resArr.getInt(1));
        res.set("message", resArr.getStr(2));
        res.set("create_time", resArr.getInt(3));
        res.set("user_id", resArr.getInt(4));
        res.set("status", 200);

        return res;
    }
}
